package vns.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import vns.client.service.ActivityService;
import vns.server.domain.Activity;
import vns.shared.ActivityDTO;
import vns.shared.ObjectDTO;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ActivityServiceImpl extends RemoteServiceServlet implements
		ActivityService {

	@Override
	public void Create(ActivityDTO activityDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Activity activity = null;
		try {
			activity = new Activity(activityDTO);
			pm.makePersistent(activity);
		} finally {
			pm.close();
		}
	}

	@Override
	public void Create(List<ActivityDTO> listactivity) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Activity> activity = new ArrayList<Activity>();
		for (ActivityDTO activityDTO : listactivity) {
			Activity temp = new Activity(activityDTO);
			activity.add(temp);
		}
		pm.makePersistentAll(activity);

	}

	@Override
	public List<ActivityDTO> LoadByObjectFromDTO(ObjectDTO object) {
		List<ActivityDTO> listDTO = new ArrayList<ActivityDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String tempkey = KeyFactory.keyToString(object.getId());
		Query q = pm.newQuery(Activity.class);
		q.setFilter("objectfrom.contains(temp)");
		q.declareParameters("String temp");
		try {
			@SuppressWarnings("unchecked")
			List<Activity> list = (List<Activity>) q.execute(tempkey);
			for (Activity activity : list) {
				listDTO.add(activity.toDTO());
			}
		} finally {
			pm.close();
		}
		return listDTO;
	}

	@Override
	public List<ActivityDTO> LoadByObjectToDTO(ObjectDTO object) {
		List<ActivityDTO> listDTO = new ArrayList<ActivityDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String tempkey = KeyFactory.keyToString(object.getId());
		Query q = pm.newQuery(Activity.class);
		q.setFilter("objectto.contains(temp)");
		q.declareParameters("String temp");
		try {
			@SuppressWarnings("unchecked")
			List<Activity> list = (List<Activity>) q.execute(tempkey);
			for (Activity activity : list) {
				listDTO.add(activity.toDTO());
			}
		} finally {
			pm.close();
		}
		return listDTO;
	}

	@Override
	public List<ActivityDTO> DTOLoadAll() {
		List<ActivityDTO> listactionDTO = new ArrayList<ActivityDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Activity.class);
		try {
			@SuppressWarnings("unchecked")
			List<Activity> res = (List<Activity>) q.execute();
			for (Activity actiontype : res) {
				Activity term = pm.detachCopy(actiontype);
				listactionDTO.add(term.toDTO());
			}
		} finally {
			pm.close();
		}
		return listactionDTO;
	}

	@Override
	public void UpdateByDTO(ActivityDTO activity) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Activity action = null;
		try {
			action = pm.getObjectById(Activity.class, activity.getId());
			action.updateFromDTO(activity);
		} finally {
			pm.close();
		}
	}

	@Override
	public List<ActivityDTO> UpdateByDTO(List<ActivityDTO> listactivity) {
		List<ActivityDTO> listOld = this.DTOLoadAll();
		List<ActivityDTO> notexist = new ArrayList<ActivityDTO>();
		for (ActivityDTO element : listactivity) {
			if (!containInList(listOld, element)) {
				notexist.add(element);
			} else {
				this.UpdateByDTO(element);
			}
		}
		return notexist;
	}

	@Override
	public void Delete(ActivityDTO activity) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Activity action = pm
					.getObjectById(Activity.class, activity.getId());
			if (action != null) {
				pm.deletePersistent(pm.getObjectById(Activity.class,
						activity.getId()));
			}
		} finally {
			pm.close();
		}
	}

	@Override
	public List<ActivityDTO> Delete(List<ActivityDTO> listactivity) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<ActivityDTO> temp = new ArrayList<ActivityDTO>();
		;
		try {
			for (ActivityDTO Action : listactivity) {

				Activity action = pm.getObjectById(Activity.class,
						Action.getId());
				if (action != null) {
					pm.deletePersistent(pm.getObjectById(Activity.class,
							Action.getId()));
				} else {
					temp.add(Action);
				}
			}
		} finally {
			pm.close();
		}
		return temp;
	}

	private boolean containInList(List<ActivityDTO> list, ActivityDTO element) {
		if (element.getId() == null)
			return false;
		for (ActivityDTO optDTO : list) {
			if (optDTO.getId().equals(element.getId()))
				return true;
		}
		return false;
	}
}
